﻿
using DevSupport.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace DevSupport.Middleware
{
    public class StaticFilesMiddleware
    {
        private readonly RequestDelegate _next; 

        public StaticFilesMiddleware(RequestDelegate next)
        {
            _next = next; 
        }

        public async Task Invoke(HttpContext context)
        {
            var url = context.GetRequestUrl();
            if (!context.IsAuthorized() && !url.Contains("login"))
            {
                context.Response.Redirect("/pages/login.html");
                return;
            } 
            await _next(context);
        }

    }
}